package com.fastloan.jn.mgt.process.tactics;

import com.fastloan.jn.mgt.application.basic.service.JnApplicationService;
import com.fastloan.jn.mgt.process.core.ReturnData;
import com.fastloan.jn.mgt.process.exception.AppApproveException;
import com.fastloan.jn.mgt.process.model.JnAppApproveUser;
import com.fastloan.jn.mgt.process.service.AppApproveService;
import com.fastloan.jn.mgt.process.service.ProcessService;
import com.fastloan.jn.mgt.process.service.impl.ApplicationAuditing;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.Map;


;import static com.fastloan.jn.mgt.application.basic.enums.ApplicationStatus.CREATE_ING;
import static com.fastloan.jn.mgt.application.basic.enums.ApplicationStatus.TO_SIGNED;


/**
 * 策略类
 * Created by chenkai on 2017/3/4.
 */
@Service
public class TacticsLinkService {

    private TacticsGenerator tacticsService;

    @Autowired
    private ProcessService processService;

    @Autowired
    private ApplicationAuditing applicationAuditing;

    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    private JnApplicationService applicationService;

    @Autowired
    private AppApproveService approveService;

    /**
     * 策略筛选
     *
     * @param tacticsType 策略类型
     * @return 执行策略结果
     */
    private TacticsGenerator setTactics(int tacticsType) {
        Map<String, TacticsGenerator> modelStrategyMap
                = applicationContext.getBeansOfType(TacticsGenerator.class);
        for (TacticsGenerator tacticsService : modelStrategyMap.values()) {
            if (tacticsType == tacticsService.getTacticsType()) {
                return tacticsService;
            }
        }
        throw new IllegalArgumentException("未找到此策略");
    }


    /**
     * 执行策略
     *
     * @param appApproveUser 用户审批信息
     * @param applicationId  进件id
     * @return 执行结果
     */
    public ReturnData runTactics(int tacticsType, JnAppApproveUser appApproveUser, String applicationId) throws AppApproveException {
        this.tacticsService = setTactics(tacticsType);
        String approvalId = tacticsService.runTactics(appApproveUser, applicationId);
        if (approvalId != null) {
            return addAppApproveUser(approvalId, applicationId);
        }
        return new ReturnData(Boolean.TRUE);
    }

    /**
     * 判断流程是否结束
     *
     * @param appApproveOneId 审批用户id
     * @param applicationId   进件id
     * @return 执行结果
     */
    ReturnData addAppApproveUser(String appApproveOneId, String applicationId) throws AppApproveException {
        ReturnData data = processService.addAppApproveUser(appApproveOneId);
        if (!data.isPass()) { //审批流程结束
            try {
                // FunctionDispatcher.getInstance().executeEnd(applicationAuditing, applicationId, true); //当流程完全走完时审批流程结束
                applicationService.modifyApplicationStatus(TO_SIGNED, applicationId); //更新进件状态为已放贷
            } catch (Exception e) {
                e.printStackTrace();
                approveService.deleteAppApprove(applicationId);
                applicationService.modifyApplicationStatus(CREATE_ING, applicationId);
                //    LogTemplate.info(this.getClass(), "审批流程未正确结束,已清理错误数据, 进件编号:" + applicationId, e.getMessage()); //当用户拒绝时审批流程结束
                //   throw new HandleOrgException("审批流程未正确结束");
            }
        }
        return data;
    }

}
